package com.itsu.app.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.RandomUtil;
import com.itsu.api.controllerApi.MyCenterControllerApi;
import com.itsu.app.service.AppMyCenterService;
import com.itsu.app.service.AppUserService;
import com.itsu.pojo.vo.CountsVo;
import com.itsu.pojo.vo.PageResult;
import com.itsu.pojo.vo.SettingVo;
import com.itsu.pojo.vo.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@Slf4j
public class MyCenterController implements MyCenterControllerApi {

    @Autowired
    AppMyCenterService appMyCenterService;

    @Override
    public ResponseEntity queryUserInfoByUserId(Long userId) {
        UserInfoVo vo = appMyCenterService.findUserByUid(userId);
        return ResponseEntity.ok(vo);
    }

    @Override
    public ResponseEntity updateUserInfoByUserId(Map<String, String> map) {
        appMyCenterService.updateUserInfo(map);
        return ResponseEntity.ok(null);
    }

    @Override
    public ResponseEntity queryCounts() {
        CountsVo countsVo = appMyCenterService.queryCounts();
        return ResponseEntity.ok(countsVo);
    }

    @Override
    public ResponseEntity updateLogo(MultipartFile headPhoto) {
        appMyCenterService.updateLogo(headPhoto);
        return ResponseEntity.ok(null);
    }

    @Override
    public ResponseEntity updateQuestions(Map<String, String> map) {
        appMyCenterService.updateQuestions(map.get("content"));
        return ResponseEntity.ok(null);
    }

    @Override
    public ResponseEntity getSettings() {
        SettingVo settingVo = appMyCenterService.querySettings();
        return ResponseEntity.ok(settingVo);
    }

    @Override
    public ResponseEntity getBlackList(Map<String, String> map) {
        PageResult result = appMyCenterService.getBlackList(map.get("page"), map.get("pagesize"));
        return ResponseEntity.ok(result);
    }

    @Override
    public ResponseEntity updateSetting(Map<String, String> map) {
        String likeNo = map.get("likeNotification");
        String pingLunNo = map.get("pinglunNotification");
        String gongGaoNo = map.get("gonggaoNotification");
        appMyCenterService.updateSetting(Convert.toBool(likeNo), Convert.toBool(pingLunNo), Convert.toBool(gongGaoNo));
        return ResponseEntity.ok(null);
    }

    @Override
    public ResponseEntity sendVerificationCode() {
        appMyCenterService.sendVerificationCode();
        return ResponseEntity.ok(null);
    }

    @Override
    public ResponseEntity checkVerificationCode(String verificationCode) {
        Map<String, Object> map = appMyCenterService.checkVerificationCode(verificationCode);
        return ResponseEntity.ok(map);
    }

    @Override
    public ResponseEntity updatePhone(String phone) {
        appMyCenterService.updatePhone(phone);
        return ResponseEntity.ok(null);
    }

    /**
     * 移除黑名单人
     * @param uid
     * @return
     */
    @DeleteMapping("blacklist/{uid}")
    public ResponseEntity deleteBlackUser(@PathVariable("uid") Long uid) {
        appMyCenterService.deleteBlackUser(uid);
        return ResponseEntity.ok(null);
    }
}
